package com.lzs.chuku.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lzs.entity.Outorder;
import com.lzs.entity.Picktask;
import com.lzs.chuku.mapper.PicktaskMapper;
import com.lzs.chuku.service.OutorderService;
import com.lzs.chuku.service.PicktaskService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzs.entity.User;
import com.lzs.util.R;
import com.lzs.util.TimeUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 刘中帅
 * @since 2020-11-23
 */
@Service
public class PicktaskServiceImpl extends ServiceImpl<PicktaskMapper, Picktask> implements PicktaskService {

    @Autowired
    private OutorderService outorderService;
    @Autowired
    private PicktaskMapper picktaskMapper;
    @Override
    public R addPickTask(List<Long> outorderids, User user) {
        try {

            for (int i = 0; i < outorderids.size(); i++) {
                Outorder outorder = outorderService.getById(outorderids.get(i));
                if (outorder.getState().equals("0")){
                    return R.error().message("您选择的订单中存在未完成明细录入的的订单");
                }else if (outorder.getState().equals("1")){
                    return R.error().message("您选中的订单中存在待审核的订单");
                }else if (outorder.getState().equals("3")){
                    return R.error().message("您选中的订单中存在已分配的订单");
                }else if (outorder.getState().equals("4")){
                    return R.error().message("您选中的订单中存在拣货中的订单");
                }else if (outorder.getState().equals("5")){
                    return R.error().message("您选中的订单中存在已出库的订单");
                }else if (outorder.getState().equals("9")){
                    return R.error().message("您选中的订单中存在已取消的订单");
                }
            }
            for (int i = 0; i < outorderids.size(); i++) {
                Outorder outorder = outorderService.getById(outorderids.get(i));
                Picktask picktask = new Picktask();
                picktask.setOutorderid(outorderids.get(i));
                picktask.setPicktaskno("JH"+ TimeUtil.gettime());
                picktask.setState("0");
                picktask.setCreateid(user.getUserId());
                picktask.setCreator(user.getName());
                List list=new ArrayList();
                list.add(outorderids.get(i));
                outorderService.changestate(list,"3");
                picktaskMapper.insert(picktask);
            }
            return R.ok().message("生成成功");
        }catch (Exception e){
            return R.error().message("生成失败");
        }
    }

    @Override
    public R findByPicktaskPage(Picktask picktask, Integer currentPage, Integer pageSize) {
        try {
            QueryWrapper<Picktask> wrapper = new QueryWrapper<>();
            if (picktask.getOutorderid() != null) {
                wrapper.like("outorderid", picktask.getOutorderid());
            }
            if (picktask.getPicktaskno() != null) {
                wrapper.like("picktaskno", picktask.getPicktaskno());
            }
            if (StringUtils.isNotEmpty(picktask.getReceiver())) {
                wrapper.like("receiver", picktask.getReceiver());
            }
            Page<Picktask> page = new Page<>(currentPage, pageSize);
            picktaskMapper.selectPage(page, wrapper);
            return R.ok().message("查找成功").data("page",page);
        }catch (Exception e){
            return R.error().message("查找失败");
        }
    }

    @Override
    public R rollBackPicktask(Long picktaskid) {
        try {
            Picktask picktask = picktaskMapper.selectById(picktaskid);
            Outorder outorder = outorderService.getById(picktask.getOutorderid());
            List<Long> list=new ArrayList<>();
            list.add(outorder.getId());
            outorderService.changestate(list,"2");
            picktaskMapper.deleteById(picktaskid);
            return R.ok().message("还原成功");
        }catch (Exception e){
            return R.error().message("还原失败");
        }
    }

    @Override
    public void changeState(String picktaskno, String s) {
        picktaskMapper.changeState(picktaskno,s);
    }
}
